// pages/editAddress/editAddress.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    milkIndex: 0,
    submitBtn: false,//提交
    submitOne: false,//20200224提交一次
    redBg: false,
    text: '获取验证码', //按钮文字
    currentTime: 91, //倒计时
    disabled: false, //按钮是否禁用
  },
  staticData: {//提交
  },
  milkChange(e) {//选择
    this.setData({
      milkIndex: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  mrBtn: function () {//设置默认
    var mrBtn = this.data.mrBtn
    mrBtn = !mrBtn;
    this.setData({
      mrBtn: mrBtn
    })
  },
  name(e) {//姓名
    this.staticData.name = e.detail.value.replace(/\s+/g, '');
    this.submitText()
  },
  tel(e) {//手机号
    this.staticData.tel = e.detail.value.replace(/\s+/g, '');
    this.submitText()
    this.telText()
  },
  address(e) {//请选择所在地区
    this.staticData.address = e.detail.value.replace(/\s+/g, '');
    this.submitText()
  },
  addressHome(e) {//详细地址
    this.staticData.addressHome = e.detail.value.replace(/\s+/g, '');
    this.submitText()
  },

  verificationCode(e) {//验证码
    this.staticData.verificationCode = e.detail.value.replace(/\s+/g, '');
    this.submitText();

  },
  submitText: function () {
    if (this.staticData.name && this.staticData.tel && this.staticData.verificationCode && this.staticData.address && this.staticData.addressHome) {
      this.setData({
        submitBtn: true
      });
    } else {
      this.setData({
        submitBtn: false
      });
    }
  },
  telText: function () {
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(this.staticData.tel)) {
      this.setData({
        redBg: false,
      });
    } else {
      this.setData({
        redBg: true
      });
    }
  },
  submit: function () {
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!this.staticData.name) {
      wx.showToast({
        title: '请输入姓名',
        icon: 'loading',
        // image:'../images/details/1.png',
        duration: 500
      })
      return;
    } else
      if (!this.staticData.tel) {
        wx.showToast({
          title: '请输入联系电话',
          icon: 'loading',
          // image:'../images/details/1.png',
          duration: 500
        })
        return;
      } else
        if (!myreg.test(this.staticData.tel)) {
          wx.showToast({
            title: '手机号有误',
            icon: 'loading',
            duration: 500
          })
          return false;
        } else
          if (!this.staticData.verificationCode) {
            wx.showToast({
              title: '请输入验证码',
              icon: 'loading',
              // image:'../images/details/1.png',
              duration: 500
            })
            return;
          } else
            if (!this.staticData.address) {
              wx.showToast({
                title: '请选择所在地区',
                icon: 'loading',
                // image:'../images/details/1.png',
                duration: 500
              })
              return;
            } else
              if (!this.staticData.addressHome) {
                wx.showToast({
                  title: '请输入详细地址',
                  icon: 'loading',
                  // image:'../images/details/1.png',
                  duration: 500
                })
                return;
              }
              else {//20200224
                this.setData({
                  submitOne: true
                });
              }
  },
  //获取验证码按钮事件
  bindButtonTap: function () {
    var that = this;
    var phone = that.staticData.tel;
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    var currentTime = that.data.currentTime;
    if (!this.staticData.tel) {
      wx.showToast({
        title: '请输入联系电话',
        icon: 'loading',
        // image:'../images/details/1.png',
        duration: 500
      })
      return;
    } else
      if (!myreg.test(this.staticData.tel)) {
        wx.showToast({
          title: '手机号有误',
          icon: 'loading',
          duration: 500
        })
        return false;
      } else {
        //当手机号正确的时候提示用户短信验证码已经发送 并禁止按钮点击导致定时器多次触发的bug
        that.setData({
          disabled: true,
        })

        wx.showToast({
          title: '短信验证码已发送',
          icon: 'none',
          duration: 2000
        });

        //设置一分钟的倒计时
        var interval = setInterval(function () {
          currentTime--; //每执行一次让倒计时秒数减一
          that.setData({
            text: '重新获取(' + currentTime + ')s', //按钮文字变成倒计时对应秒数
            redBg: false,
            disabled: true,
          })

          //如果当秒数小于等于0时 停止计时器 且按钮文字变成重新发送 且按钮变成可用状态 倒计时的秒数也要恢复成默认秒数 即让获         取验证码的按钮恢复到初始化状态只改变按钮文字

          if (currentTime <= 0) {
            clearInterval(interval)
            that.setData({
              text: '重新获取',
              currentTime: 91,
              disabled: false,
              redBg: true
            })
          }
        }, 1000);
      };


  },
})